D:\a\tools.proto\tools.proto\dynamic\src\field\string.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use bp3d_protoc::model::protocol::Endianness; |
30 | | use crate::buffer::{BufferView, Builder}; |
31 | | use crate::component::{Component, ComponentType, util::DiscoverTool}; |
32 | | use crate::component::factory::SizeType; |
33 | | use crate::field::codec::{ByteCodecBE, ByteCodecLE, Codec}; |
34 | | |
35 | | pub struct NullTerminatedString; |
36 | | |
37 | | struct VarcharStringInner<T>(T); |
38 | | |
39 | | impl<T: Codec> Component for VarcharStringInner<T> { |
40 | 0 | fn read(&self, view: &mut BufferView, _: &mut DiscoverTool) -> bp3d_proto::message::Result<()> { |
41 | 0 | let len = self.0.read(view.buffer().as_bytes()); |
42 | 0 | let data = &mut view["data"]; |
43 | 0 | data.location_mut().size = len as _; |
44 | 0 | Ok(()) |
45 | 0 | } |
46 | | |
47 | 0 | fn shape(&self, view: &mut BufferView, _: &Vec<BufferView>) -> bp3d_proto::message::Result<()> { |
48 | 0 | let len = view["data"].buffer().len(); |
49 | 0 | self.0.write(view["len"].buffer_mut().as_bytes_mut(), len as _); |
50 | 0 | Ok(()) |
51 | 0 | } |
52 | | } |
53 | | |
54 | | impl Component for NullTerminatedString { |
55 | 6 | fn read(&self, view: &mut BufferView, _: &mut DiscoverTool) -> bp3d_proto::message::Result<()> { |
56 | 6 | let mut motherfuckingrust = 0; |
57 | 52 | while view.buffer().as_bytes()[motherfuckingrust] != 0x0 { Branch (57:15): [Folded - Ignored]
Branch (57:15): [True: 46, False: 6]
|
58 | 46 | motherfuckingrust += 1; |
59 | 46 | } |
60 | 6 | view.location_mut().size = motherfuckingrust + 1; |
61 | 6 | Ok(()) |
62 | 6 | } |
63 | | |
64 | 6 | fn shape(&self, view: &mut BufferView, _: &Vec<BufferView>) -> bp3d_proto::message::Result<()> { |
65 | 6 | if view.buffer().is_empty() { Branch (65:12): [Folded - Ignored]
Branch (65:12): [True: 1, False: 5]
|
66 | 1 | view.buffer_mut().set_bytes(b"\0"); |
67 | 1 | return Ok(()) |
68 | 5 | } |
69 | 5 | let motherfuckingrust = view.buffer().len() - 1; |
70 | 5 | if view.buffer().as_bytes()[motherfuckingrust] != 0x0 { Branch (70:12): [Folded - Ignored]
Branch (70:12): [True: 2, False: 3]
|
71 | 2 | view.buffer_mut().as_bytes_mut()[motherfuckingrust] = 0x0; |
72 | 3 | } |
73 | 5 | Ok(()) |
74 | 6 | } |
75 | | } |
76 | | |
77 | | pub struct VarcharString(pub SizeType); |
78 | | |
79 | | impl ComponentType for VarcharString { |
80 | 0 | fn build(&self, builder: Builder<'static>) -> Builder<'static> { |
81 | 0 | if self.0.get_endianness() == Endianness::Little { Branch (81:12): [Folded - Ignored]
Branch (81:12): [True: 0, False: 0]
|
82 | 0 | builder.component(VarcharStringInner(ByteCodecLE)).add_child(Builder::new("len").fixed(0, self.0.get_size())) |
83 | 0 | .add_child(Builder::new("data")) |
84 | | } else { |
85 | 0 | builder.component(VarcharStringInner(ByteCodecBE)).add_child(Builder::new("len").fixed(0, self.0.get_size())) |
86 | 0 | .add_child(Builder::new("data")) |
87 | | } |
88 | 0 | } |
89 | | } |
90 | | |
91 | | impl ComponentType for NullTerminatedString { |
92 | 4 | fn build(&self, builder: Builder<'static>) -> Builder<'static> { |
93 | 4 | builder.component(NullTerminatedString) |
94 | 4 | } |
95 | | } |